//
//  GourmetViewController.m
//  ForengnMenu
//
//  Created by wenzhaoxu on 16/1/22.
//  Copyright © 2016年 余旺. All rights reserved.
//

#import "GourmetViewController.h"
#import "GourmetTableViewCell.h"
#import "MenuContentViewController.h"
#import "CExpandHeader.h"

@interface GourmetViewController ()<UITableViewDataSource,UITableViewDelegate,GourmetTableViewCellDelegate>

@property (weak, nonatomic) IBOutlet UITableView *myTableView;

@property BOOL isOpen;

@property BOOL isOk;

@property int praiseNumber;

@property NSArray * recipeArray;

@end

@implementation GourmetViewController{

    CExpandHeader *_header;
    __weak UIImageView *_expandView;

}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 45)];
    
    NSDictionary * info =self.arrayUser[3];
    
    label.text = info[@"titerName"];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.textColor = [UIColor whiteColor];
    
    self.navigationItem.titleView = label;
    
    UIButton * rightBnt = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBnt.frame = CGRectMake(0, 0, 30, 30);
    [rightBnt setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    [rightBnt addTarget:self action:@selector(returnAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBnt];

    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 22, 22);
    [button setImage:[UIImage imageNamed:@"Icon_search1"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

    self.myTableView.tableFooterView = [UIView new];
    
    //指定的位置
    NSString * RecipeModel = [[NSBundle mainBundle] pathForResource:@"RecipeModel" ofType:@"plist"];
    //大数组
    self.recipeArray = [NSArray arrayWithContentsOfFile:RecipeModel];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 227)];
     NSDictionary * info1 =self.arrayUser[3];
    [imageView setImage:[UIImage imageNamed:info1[@"headerImageView"]]];
    
    _header = [CExpandHeader expandWithScrollView:_myTableView expandView:imageView];
    

}
-(void)returnAction{

    [self.navigationController popViewControllerAnimated:YES];
}
-(void)searchAction{

    [self performSegueWithIdentifier:@"sou" sender:nil];
}
//当界面消失之前调用
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
    //让导航栏透明
    UIImage *image = [UIImage imageNamed:@"Icon_white_background"];
    [self.navigationController.navigationBar setBackgroundImage:image
                                                  forBarMetrics:UIBarMetricsDefault];
    //让分割线透明
    [self.navigationController.navigationBar setShadowImage:[UIImage imageNamed:@""]];
    
}

//视图将要出现时
-(void)viewWillAppear:(BOOL)animated{
    //让导航栏透明
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    
    //让分割线透明
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arrayUser.count-1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * identifier = @"Cell1";
    
    GourmetTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    NSDictionary * info =self.arrayUser[indexPath.row];
    
    cell.imageViewName.image = [UIImage imageNamed:info[@"foodImageView"]];
    
    cell.foodName.text = info[@"foodName"];
    
    cell.nameLabel.text = info[@"userName"];
    
    cell.myTextLabel.text = info[@"foodText"];
    
    cell.zanLabel.text = info[@"zanLabel"];
    
    self.praiseNumber = [[NSString stringWithFormat:@"%@",info[@"zanLabel"]] intValue];
    
    cell.delegate = self;
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary * info =self.arrayUser[indexPath.row];
    
    NSString * string = info[@"foodName"];
    
    NSArray * array;
    if ([string isEqualToString:@"凯撒沙拉"]) {
         array = self.recipeArray[1];
    }else{
         array = self.recipeArray[4];
    }
    
    
    [self performSegueWithIdentifier:@"foodConten" sender:array[0]];
}
-(void)shouChangAction:(GourmetTableViewCell *)cell{
    if (_isOpen) {
        [cell.shouChangBnt setImage:[UIImage imageNamed:@"Icon_collection"] forState:UIControlStateNormal];
        
        _isOpen =NO;
    }else{
        
        [cell.shouChangBnt setImage:[UIImage imageNamed:@"Icon_collectionYES"] forState:UIControlStateNormal];

        _isOpen =YES;
        
    }


}

-(void)zanAction:(GourmetTableViewCell *)cell{
    if (_isOk) {
        [cell.zanBnt setImage:[UIImage imageNamed:@"Icon_praiseNO.png"] forState:UIControlStateNormal];
        _praiseNumber =_praiseNumber-1;
        
        cell.zanLabel.text = [NSString stringWithFormat:@"%d",_praiseNumber];
        
        _isOk = NO;
    }else{
        [cell.zanBnt setImage:[UIImage imageNamed:@"Icon_praiseYES.png"] forState:UIControlStateNormal];
        _praiseNumber =_praiseNumber+1;
        
        cell.zanLabel.text = [NSString stringWithFormat:@"%d",_praiseNumber];
        _isOk =YES;
    }

}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    if ([segue.identifier isEqualToString:@"foodConten"]) {
        
        MenuContentViewController * content = segue.destinationViewController;
        
        content.recipeArray = sender;

    }


}
@end
